package org.edu.modules.kwgl.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.util.SpringContextUtils;
import org.edu.modules.kwgl.api.CommonApi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * excel相关处理工具类
 * 2020-05-6
 * by cyn
 */
public class CommonAipUtils {
    private static CommonApi commonApi = SpringContextUtils.getBean(CommonApi.class);

    private static String ID_KEY="id";
    private static String ZYBNAME_KEY="yxmc";
    private static String GRADENAME_KEY="njmc";
    private static String ZYNAME_KEY="zymc";
    private static String BANJINAME_KEY="xzbmc";
    private static String XQNAME_KEY="xqmc";




    private static String LIST_KEY="list";

    /**
     * 返回专业部id，名称map  ,yxmc
     * @return
     */
    public static Map<Integer,String> getZybIdNameMap(){
        Map<Integer,String> map=new HashMap<>();
        try {
            Result<List<JSONObject>> result = commonApi.queryFacultyList(CommonApi.COMMON_TOKEN_KEY);
            if(result.getResult()!=null){
                for(JSONObject item:result.getResult()){
                    if(item.containsKey(ID_KEY) && item.containsKey(ZYBNAME_KEY)){
                        map.put(item.getInteger(ID_KEY),item.getString(ZYBNAME_KEY));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
    /**
     * 返回专业部名称，idmap
     * @return
     */
    public static Map<String,Integer> getZybNameIdMap(){
        Map<String,Integer> map=new HashMap<>();
        try {
            Result<List<JSONObject>> result = commonApi.queryFacultyList(CommonApi.COMMON_TOKEN_KEY);
            if(result.getResult()!=null){
                for(JSONObject item:result.getResult()){
                    if(item.containsKey(ID_KEY) && item.containsKey(ZYBNAME_KEY)){
                        map.put(item.getString(ZYBNAME_KEY),item.getInteger(ID_KEY));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 返回校区名称，id map
     * @return
     */
    public static Map<String,Integer> getXiaoquNameId(){
        Map<String,Integer> map=new HashMap<>();
        try {
            Result<List<JSONObject>> result = commonApi.queryCampusList(CommonApi.COMMON_TOKEN_KEY);
            if(result.getResult()!=null){
                for(JSONObject item:result.getResult()){
                    if(item.containsKey(ID_KEY) && item.containsKey(XQNAME_KEY)){
                        map.put(item.getString(XQNAME_KEY),item.getInteger(ID_KEY));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 返回学期名称，id map
     * @return
     */
    public static Map<Integer,String> getXueqiIdName(){
        Map<Integer,String> map=new HashMap<>();
        try {
            Result<List<JSONObject>> result = commonApi.querySemesterList(CommonApi.COMMON_TOKEN_KEY);
            if(result.getResult()!=null){
                for(JSONObject item:result.getResult()){
                    if(item.containsKey(ID_KEY) && item.containsKey(XQNAME_KEY)){
                        map.put(item.getInteger(ID_KEY),item.getString(XQNAME_KEY));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 返回学期名称，id map
     * @return
     */
    public static Map<String,Integer> getXueqiNameId(){
        Map<String,Integer> map=new HashMap<>();
        try {
            Result<List<JSONObject>> result = commonApi.querySemesterList(CommonApi.COMMON_TOKEN_KEY);
            if(result.getResult()!=null){
                for(JSONObject item:result.getResult()){
                    if(item.containsKey(ID_KEY) && item.containsKey(XQNAME_KEY)){
                        map.put(item.getString(XQNAME_KEY),item.getInteger(ID_KEY));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 返回年级id，名称map  ,yxmc
     * @return
     */
    public static Map<Integer,String> getGradeIdNameMap(){
        Map<Integer,String> map=new HashMap<>();
        try {
            Result<List<JSONObject>> result = commonApi.queryGradeList(CommonApi.COMMON_TOKEN_KEY);
            if(result.getResult()!=null){
                for(JSONObject item:result.getResult()){
                    if(item.containsKey(ID_KEY) && item.containsKey(GRADENAME_KEY)){
                        map.put(item.getInteger(ID_KEY),item.getString(GRADENAME_KEY));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 根据id查专业名称
     * @param id
     * @return
     */
    public static String getZyById(Integer id){
        if(id!=null && id!=0){
            try {
                Result<JSONObject> result = commonApi.querySpecialtyById(CommonApi.COMMON_TOKEN_KEY, id);
                if (result.getResult() != null && result.getResult().containsKey(ZYNAME_KEY)) {
                    return result.getResult().getString(ZYNAME_KEY);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;

    }

    /**
     * 根据id查专业
     * @param id
     * @return
     */
    public static JSONObject getZyObjById(Integer id){
        if(id!=null && id!=0){
            try {
                Result<JSONObject> result = commonApi.querySpecialtyById(CommonApi.COMMON_TOKEN_KEY, id);
                if (result.getResult() != null) {
                    return result.getResult();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;

    }

    /**
     * 根据id查班级名称
     * @param id
     * @return
     */
    public static String getBanjiById(Integer id){
        if(id!=null && id!=0){
            try {
                Result<JSONObject> result = commonApi.queryBanJiById(CommonApi.COMMON_TOKEN_KEY,id);
                if(result.getResult()!=null && result.getResult().containsKey(BANJINAME_KEY)){
                    return result.getResult().getString(BANJINAME_KEY);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 根据班级名称匹配班级
     * @param name
     * @return
     */
    public static JSONObject getBanjiByName(String name){
        if(StringUtils.isNotEmpty(name)){
            try {
                Result<JSONObject> result = commonApi.queryBanJiPageList(CommonApi.COMMON_TOKEN_KEY,name);
                if(result.getResult()!=null &&  result.getResult().containsKey(LIST_KEY) &&  result.getResult().get(LIST_KEY)!=null){
                    return result.getResult().getJSONArray(LIST_KEY).getJSONObject(0);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 根据学号姓名取学生
     * @param xm
     * @return
     */
    public static JSONObject getXueshengByXh(String xh,String xm){
        if(StringUtils.isNotEmpty(xh) && StringUtils.isNotEmpty(xm)){
            try {
                Result<JSONObject> result = commonApi.getStudentPageList(CommonApi.COMMON_TOKEN_KEY,xh,xm);
                if(result.getResult()!=null &&  result.getResult().containsKey(LIST_KEY) &&  result.getResult().get(LIST_KEY)!=null){
                    return result.getResult().getJSONArray(LIST_KEY).getJSONObject(0);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 根据班级ID取学生
     * @param bjid
     * @return
     */
    public static JSONArray getXueshengByBjid(Integer bjid){
        if(bjid!=null){
            try {
                Result<JSONObject> result = commonApi.getStudentPageList(CommonApi.COMMON_TOKEN_KEY,bjid,9999);
                if(result.getResult()!=null &&  result.getResult().containsKey(LIST_KEY) &&  result.getResult().get(LIST_KEY)!=null){
                    return result.getResult().getJSONArray(LIST_KEY);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }
}
